package cn.com.dhcc.turbo.dao.registry;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.AbstractCrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.ModuleFeatureBeanRowWapper;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.turbo.entity.registry.DataCategory;

/**
 * 数据分类dao
 * 
 * @日期：2014-9-29下午4:31:19
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Repository
public class DataCategoryDao extends AbstractCrudDao<DataCategory> {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.TURBO);

	
	@Override
	public PagerData<DataCategory> getPagerList(Pager pager,
			WhereCondition parameter) throws DaoException {
		String sql="select t1.*,t2.code domainCode,t2.name domainName from "+ SI_TABLE("turbo_data_category") + " t1 " +
				"  left join "+ SI_TABLE("turbo_domain_info") + " t2 on t1.domain_id=t2.id";
		return CM.getDao().getPagerList("", sql, new ModuleFeatureBeanRowWapper<DataCategory>(DataCategory.class), pager, parameter, new Object[]{});
	}

/**
 * 获取子数据分类
 * @param pid
 * @return
 * @throws DaoException
 */
	public List<DataCategory> getListByPid(String pid) throws DaoException{
		try {
			final String sql = "select t1.id,t1.data_name,t1.pid,t1.sys_id,t1.data_code, count(t2.id) count"
					+ "   from "+ SI_TABLE("turbo_data_category") + " t1"
					+ "   left join "+ SI_TABLE("turbo_data_category") + " t2"
					+ "   on t1.id = t2.pid where t1.status = 1 and t1.pid=?"
					+ "    group by t1.id,t1.data_code,t1.data_name,t1.pid,t1.sys_id,t1.data_code "
					+ "	 order by t1.data_code";
			return CM.getDao().list("获取子数据分类", sql, new ModuleFeatureBeanRowWapper<DataCategory>(DataCategory.class),new Object[] { pid });
		} catch (DaoException e) {
			throw new DaoException("获取子数据分类！", e);
		}
	}
	public String getMaxDataCodeByPid(String pid) throws DaoException{
		try {
			final String sql = "select   max(data_code)  code "
					+ " from " + SI_TABLE("turbo_data_category")
					+ " where ((? is null and pid is null) or pid=?) ";
			return  CM.getDao().queryForString("获取最大节点编码", sql,new Object[] { pid,pid }) ;
		} catch (DaoException e) {
			throw new DaoException("获取最大节点编码失败！", e);
		}
	}
	public List<DataCategory> getListByDomainId(String domainId) throws DaoException{
		try {
			final String sql = "select t1.id,t1.data_name,t1.pid, count(t2.id) count"
					+ "   from "+ SI_TABLE("turbo_data_category") + " t1"
					+ "   left join "+ SI_TABLE("turbo_data_category") + " t2"
					+ "   on t1.id = t2.pid where t1.domain_id=? and t1.pid is null"
					+ "    group by t1.id,t1.data_code,t1.data_name,t1.pid "
					+ "	 order by t1.data_code";
			return CM.getDao().list("获取子数据分类", sql, new ModuleFeatureBeanRowWapper<DataCategory>(DataCategory.class),new Object[] { domainId });
		} catch (DaoException e) {
			throw new DaoException("获取子数据分类！", e);
		}
	}
	/**
	 * 判断数据分类是否加密
	 * @param dataCode
	 * @return
	 * @throws DaoException
	 */
	public boolean isEncode(String dataCode)throws DaoException{
		boolean flag = false;
		try {
			String sql = "select * from " + SI_TABLE("turbo_data_category") +" where data_code=?";
			DataCategory dc =  CM.getDao().get("获取数据分类信息", sql, new ModuleFeatureBeanRowWapper<DataCategory>(DataCategory.class), new Object[] { dataCode });
			if(dc != null && "1".equals(dc.getEncodeTls())){
				flag = true;
			}
			return flag;
		} catch (DaoException e) {
			logger.error("获取数据分类信息失败", e);
			throw new DaoException("获取数据分类信息失败", e);
		}
	}
	public String getMaxDataCodeByDomainId(String domainId) throws DaoException{
		try {
			String sql = "select max(data_code) from " + SI_TABLE("turbo_data_category") +"where domain_id=?";
			return  CM.getDao().queryForString("获取数据分类信息", sql, new Object[] { domainId });
		} catch (DaoException e) {
			logger.error("获取数据分类信息失败", e);
			throw new DaoException("获取数据分类信息失败", e);
		}
	}
	
	/**
	 * 获取适配器列表
	 * @param pager
	 * @param condition
	 * @return
	 * @throws DaoException
	 */
	public PagerData<DataCategory> getDataCategoryList(Pager pager,WhereCondition condition,String orgCode)  throws DaoException {
		String sql = " select t1.*,t2.name domainName,case when t1.org_code = '"+orgCode+"' and t1.dc_type=1 then 1 else  0 end edit from " + SI_TABLE("turbo_data_category") +" t1"
							+" left join " + SI_TABLE("turbo_domain_info")+" t2 on t1.domain_id = t2.id";
		return CM.getDao().getPagerList("获取适配器列表", sql, new ModuleFeatureBeanRowWapper<DataCategory>(DataCategory.class), pager,condition,new Object[]{});
	}
	
/**
 * 取数据分类最大编码
 * @param adapterType
 * @param orgCode
 * @return
 * @throws DaoException
 */
	public long localMaxCode(String adapterType,String orgCode) throws DaoException {
		try {
			WhereCondition condition = new WhereCondition();
			condition.where().eq("dc_type", adapterType);
			if("1".equals(adapterType)){
				condition.and().like("data_code", orgCode+"%");
			}
			final String sql = "select max(data_code)   from " + SI_TABLE("turbo_data_category");
			long maxCode =(CM.getDao().queryForLong("获取最大数据分类编码", sql,condition, new Object[]{}) != null ? CM.getDao().queryForLong("获取最大数据分类编码", sql,condition, new Object[]{}):0)+1;
			return  maxCode ;
		} catch (DaoException e) {
			throw new DaoException("获取数据分类最大编码失败！", e);
		}
	}
}
